package service;

import beans.CarInfo;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by yanglq on 2018/4/9.
 */
public class SaveLogService {
    private final static Logger logger = LoggerFactory.getLogger(SaveLogService.class);

    //保存车辆进出记录
    public void saveLog(CarInfo inf) throws IOException {

        List<NameValuePair> info;
        APIService apiService = new APIService();
        switch (inf.getCarStatus()) {
            case 0:
                info = transferToSaveLogInfo(inf, 0);
                apiService.dosaveLog(info);
                logger.info("车主{}入场时间为{}", inf.getCarOwnerId(), inf.getEnterTimeStr());
                break;
            case 1:
                info = transferToSaveLogInfo(inf, 1);
                apiService.dosaveLog(info);
                logger.info("车主{}出场时间为{}", inf.getCarOwnerId(), inf.getExitTimeStr());
                break;
            case 2:
                info = transferToSaveLogInfo(inf, 1);
                apiService.dosaveLog(info);
                logger.info("车主{}出场时间为{}", inf.getCarOwnerId(), inf.getExitTimeStr());
                break;
            default:
                logger.error("车主{}车辆状态错误", inf.getCarOwnerId());
                break;
        }
    }

    private List<NameValuePair> transferToSaveLogInfo(CarInfo inf, Integer status) {
        List<NameValuePair> nvps = new ArrayList<>();
        if (inf != null) {
            nvps.add(new BasicNameValuePair("sEmpl_c", String.valueOf(inf.getCarOwnerId())));
            //// TODO: 2018/4/9 通过何种方式获取大门信息
//         nvps.add(new BasicNameValuePair("sGate", String.valueOf(inf.getCarOwnerId())));
            if (status == 0) {
                nvps.add(new BasicNameValuePair("sDate", inf.getEnterTimeStr()));
                nvps.add(new BasicNameValuePair("nEnter", "1"));
            } else {
                nvps.add(new BasicNameValuePair("sDate", inf.getExitTimeStr()));
                nvps.add(new BasicNameValuePair("nEnter", "0"));
            }
        }
        return nvps;
    }
}
